package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Library extends Activity implements OnClickListener {
     // http://webspace1.apiit.lk/gims/library/Links.asp 
	 // txtView_2131230720:/html[1]/body[1]/p[4]/a[1]---=== 
	Button search;
	Button reservations;
	Button fines;
	Button renewals;
    TextView test;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_library);

		search = (Button) findViewById(R.id.btnLmsAllCourses);
		reservations = (Button) findViewById(R.id.btnLibraryRes);
		fines = (Button) findViewById(R.id.btnButton3);
		renewals = (Button) findViewById(R.id.btnButton4);
		test = (TextView) findViewById(R.id.txtResult);
		
		Intent intename = getIntent();
		String value = intename.getStringExtra("title");
		test.setText(value);
		
		search.setOnClickListener(this);
		reservations.setOnClickListener(this);
		fines.setOnClickListener(this);
		renewals.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.library, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == search) {
			// OPAC Search - http://webspace1.apiit.lk/gims/library/LibrarySearchMain.asp  POST 
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/LibrarySearchMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("name","//title---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Intent i = new Intent(this, SearchTab.class);
			startActivity(i);
		}
		if (v == reservations) {
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/ReservationMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("message","/html[1]/body[1]/p[1]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String testResult = respones.getResultsMap().get("message");
			
			Toast.makeText(getApplicationContext(), testResult,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, ReservationsTab.class);
			i.putExtra("message", testResult);
			startActivity(i);
		}
		if (v == fines) {
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/LibraryFineMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("patronNo","/html[1]/body[1]/table[1]/tr[1]/td[1]---");
			xpathMap.put("Name","/html[1]/body[1]/table[1]/tr[2]/td[1]---");
			xpathMap.put("Status","/html[1]/body[1]/table[1]/tr[3]/td[1]---");
			xpathMap.put("expDate","/html[1]/body[1]/table[1]/tr[4]/td[1]---");
			xpathMap.put("message","/html[1]/body[1]/p[1]---");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String patronNo = respones.getResultsMap().get("patronNo");
			String patronNumArray [] = patronNo.split("&");
			String patronNumber = patronNumArray[0];
			
			String namevalue = respones.getResultsMap().get("Name");
			String nameNumArray [] = namevalue.split("&");
			String name = nameNumArray[0];
			
			String statusvalue = respones.getResultsMap().get("Status");
			String statusNumArray [] = statusvalue.split("&");
			String status = statusNumArray[0];
			
			String datevalue = respones.getResultsMap().get("expDate");
			String dateNumArray [] = datevalue.split("&");
			String expDate = dateNumArray[0];
			
			String message = respones.getResultsMap().get("message");
			
			Toast.makeText(getApplicationContext(), patronNumber + name + status + expDate + message,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, FinesTab.class);
			i.putExtra("patronNo", patronNumber);
			i.putExtra("name", name);
			i.putExtra("status", status);
			i.putExtra("expDate", expDate);
			i.putExtra("message", message);
			startActivity(i);
		}
		if (v == renewals) {
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/LoanItemsMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("patronNo","/html[1]/body[1]/table[1]/tr[1]/td[1]---");
			xpathMap.put("Name","/html[1]/body[1]/table[1]/tr[2]/td[1]---");
			xpathMap.put("Status","/html[1]/body[1]/table[1]/tr[3]/td[1]---");
			xpathMap.put("expDate","/html[1]/body[1]/table[1]/tr[4]/td[1]---");
			xpathMap.put("message","/html[1]/body[1]/p[1]---");

			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String patronNo = respones.getResultsMap().get("patronNo");
			String patronNumArray [] = patronNo.split("&");
			String patronNumber = patronNumArray[0];
			
			String namevalue = respones.getResultsMap().get("Name");
			String nameNumArray [] = namevalue.split("&");
			String name = nameNumArray[0];
			
			String statusvalue = respones.getResultsMap().get("Status");
			String statusNumArray [] = statusvalue.split("&");
			String status = statusNumArray[0];
			
			String datevalue = respones.getResultsMap().get("expDate");
			String dateNumArray [] = datevalue.split("&");
			String expDate = dateNumArray[0];
			
			String message = respones.getResultsMap().get("message");
			
			Toast.makeText(getApplicationContext(), patronNumber + name + status + expDate + message,
					Toast.LENGTH_LONG).show();
			
			Intent i = new Intent(this, RenewalsTab.class);
			i.putExtra("patronNo", patronNumber);
			i.putExtra("name", name);
			i.putExtra("status", status);
			i.putExtra("expDate", expDate);
			i.putExtra("message", message);
			startActivity(i);
		}


	}

}
